package com.itheima.Service.impl;

import com.itheima.Mapper.BannerMapper;
import com.itheima.Service.BannerService;
import com.itheima.pojo.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public List<Banner> page() {


        return bannerMapper.list();
    }

    @Override
    public Banner get(Integer id) {
        return bannerMapper.get(id);
    }

    @Override
    public void add(Banner banner) {
        banner.setCreateTime(LocalDateTime.now());
        banner.setUpdateTime(LocalDateTime.now());
        // 默认启用状态
        if (banner.getStatus() == null) {
            banner.setStatus("1");
        }
        bannerMapper.insert(banner);
    }

    @Override
    public void update(Banner banner) {
        banner.setUpdateTime(LocalDateTime.now());
        bannerMapper.update(banner);
    }

    @Override
    public void updateStatus(Integer id, String status) {
        bannerMapper.updateStatus(id, status);
    }

    @Override
    public void sort(List<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            bannerMapper.updateSort(ids.get(i), i + 1);
        }
    }

    @Override
    public void delete(Integer id) {
        bannerMapper.delete(id);
    }

    @Override
    public List<Banner> listActive() {
        return bannerMapper.listActive();
    }
}